

//二级菜单
const childRouter = [
  {
    path: 'courseList',
    component: () => import('../views/main/courseList'),
    meta: {
      // title: '课程管理',
      icon: 'el-icon-home',
      item: 'course'
    }
  },
  {
    path: 'courseAdd',
    component: () => import('../views/main/courseAdd'),
    meta: {
      // title: '添加课程',
      icon: 'el-icon-home',
      item: 'course'
    }
  },
  {
    path: 'student',
    component: () => import('../views/main/student'),
    meta: {
      title: '学员',
      icon: 'el-icon-home',
      item: 'users'
    }
  },
  {
    path: 'teacher',
    component: () => import('../views/main/teacher'),
    meta: {
      title: '讲师',
      icon: 'el-icon-home',
      item: 'users'
    }
  },
];
//一级菜单
const mainRouter = [
  {
    path: 'home',
    component: () => import('../views/main/home'),
    meta: {
      title: '首页',
      icon: 'el-icon-home',
      group: false
    }
  },
  {
    path: 'course',
    component: () => import('../views/main/teacher'),
    meta: {
      title: '课程管理',
      icon: 'el-icon-home',
      group: 'course'
    }
  },
  {
    path: 'users',
    component: () => import('../views/main/teacher'),
    meta: {
      title: '用户管理',
      icon: 'el-icon-home',
      group: 'users'
    }
  },
  {
    path: 'addQuestion',
    component: () => import('../views/main/questionAdd'),
    meta: {
      title: '添加试题',
      icon: 'el-icon-home',
      group: false
    }
  },
  ...childRouter,

  {
    path: '',
    redirect: 'home'
  }
];


export default mainRouter;
//给外面暴露数据
// 1. 哪些是一级菜单
// 2. 哪些是二级菜单
// 3. 装到数组里

export function getNavData() {
  //1 . 过滤掉重定向
  let routes = mainRouter.filter(item => ('meta' in item));

  // 2. 过滤掉 没有title的
  routes = routes.filter(item => ('title' in item.meta));

  // 哪些是一级菜单

  let subMenu = routes.filter(item => ('group' in item.meta));

  // 3. 哪些是二级菜单
  let itemMenus = routes.filter(item => ('item' in item.meta));

  //给每个subMenu添加child
  itemMenus.forEach(item => subMenu.find(val => val.meta.group === item.meta.item).child = []);

  // 4. 将二级路由添加到一级里面
  itemMenus.forEach(item => {
    // item  group
    let sub = subMenu.find(val => val.meta.group === item.meta.item);
    sub.child.push(item);
  });


  return subMenu;

}

// window.getNavData = getNavData;
